package com.auerdi.ui.fragment.e_my.orders.childs;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.auerdi.R;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.Order;
import com.example.egghttp.retrofit.beans.banner.OrrderList;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/6/30.
 */
public class OrderBWaitFragment extends SupportFragment {

    Unbinder unbinder;
    @BindView(R.id.recy)
    RecyclerView recy;


    ArrayList<Order> mOrderList = new ArrayList<>();
    private OrrderList mOrders;

    public static OrderBWaitFragment newInstance() {

        Bundle args = new Bundle();
        OrderBWaitFragment fragment = new OrderBWaitFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.officer_dis, container, false);
        unbinder = ButterKnife.bind(this, view);
        recy.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        recy.setHasFixedSize(true);
        recy.setAdapter(new CommonAdapter<Order>(getContext(), R.layout.item_order_all, mOrderList) {
            @Override
            protected void convert(ViewHolder viewHolder, final Order item, int position) {


//                ImageView img = viewHolder.getView(R.id.img);
                ImageView gpic = viewHolder.getView(R.id.gpic);
//
                viewHolder.setText(R.id.order_id, item.getOrder_sn());

                if (mOrders != null)
                    viewHolder.setText(R.id.status, mOrders.getOrder_status_list().get(Integer.parseInt(item.getOrder_status())));
//                viewHolder.setText(R.id.title, item.getTitle());
//                viewHolder.setText(R.id.stitle, item.getNick());
//                viewHolder.setText(R.id.like, item.getDianzan_count());
//                Picasso.get().load(item.getPic_zhutu()).error(R.mipmap.vip).placeholder(R.mipmap.logo).into(img);

//                Picasso.get().load(item.getGoods_list().get(0).getProduct_country_logo()).error(R.mipmap.epta).placeholder(R.mipmap.epta).into(gpic);

//                viewHolder.getConvertView().setOnClickListener(v ->
//                        EventBusActivityScope.getDefault((Activity) viewHolder.getConvertView().getContext()).post(ADProductDetailRedFragment.newInstance(new Product(), item.getId())));


            }
        });


        refreshData();


        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    private void refreshData() {
        //订单类型：0全部1待付款2待成团3代发货4待收货5待评价

        RetrofitClient.getPDAClient().create(AdService.class).orderLists("1", "0", "0")
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<OrrderList>>() {
                               @Override
                               public void onCompleted() {

                               }

                               @Override
                               public void onError(Throwable e) {

                               }

                               @Override
                               public void onNext(BOb<OrrderList> orrderListBOb) {
                                   ArrayList<Order> orderList = orrderListBOb.getBody().getOrder_list();
                                   mOrderList.clear();
                                   mOrderList.addAll(orderList);


                                   mOrders = orrderListBOb.getBody();
                                   recy.getAdapter().notifyDataSetChanged();

                               }
                           }


                );


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        EventBus.getDefault().register(this);
//    }
//
//    @Override
//    public void onStop() {
//        super.onStop();
//        EventBus.getDefault().unregister(this);
//    }


}
